package com.lzs.collection.linkedlist;

import java.util.Iterator;
import java.util.LinkedList;

public class LinkedList_crud {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        /*
        * 此时执行无参构造器
        * public LinkedList(){}
        * 这时候linkedList的属性 first=null,last=null;
        * */
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        /*添加执行的方法
        * public boolean add(E e){
        * linkLast(e);
        * return true;
        * }
        * 将新的结点，加入到双向链表的最后
        * void linkLast(E e){
        *    final Node<E> l=last;
        *    final Node<E> newNode=new Node<> (l,e,null);
        *    last=newNode;
        *    if(l==null)
        *      first=newNode;
        *    else
        *      l.next=newNode;
        *    size++;
        *    modCount++;
        * }
        * */
        System.out.println(linkedList);
        //删除
        /*remove底层
        * 1、执行removeFirst();
        * public E remove(){
        * return removeFirst;
        * }
        * 2、执行：
        * public E removeFirst(){
        *    final Node<E> f=first;
        *    if(f==null)
        *       throw new NoSuchElementException();
        *    return unlinkFirst(f);
        *
        * }
        * 3、再执行unlinkFirst,将f指向的双向链表的第一个结点拿掉
        * private E unlinkFirst(Node<E> f){
        *    final E element =f.item;
        *    final Node<E> next=f.next;
        *    f.item=null;
        *    f.next=null;
        *    first=next;
        *    if(next==null)
        *       last=null;
        *    else
        *       next,prev=null;
        *    size--;
        *    modCount++;
        *    return element;
        *
        * }
        * */
        linkedList.remove();//默认删除第一个结点
        linkedList.remove(2);//删除第2个结点
        System.out.println(linkedList);
        //修改
        linkedList.set(1,10);
        System.out.println(linkedList);
        //得到某个结点的值
        Object o = linkedList.get(1);
        System.out.println(o);

        //迭代器遍历
        Iterator iterator = linkedList.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(next);
        }
        //增强for循环遍历
        for (Object object : linkedList) {
            System.out.println(object);
        }
        //for循环遍历
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
    }
}
